package com.sdcx.agrobank.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sdcx.agrobank.R;
import com.sdcx.agrobank.SettingActivity;
import com.sdcx.agrobank.base.BaseFragment;
import com.sdcx.agrobank.event.myEvent;
import com.sdcx.agrobank.event.xykEvent;
import com.sdcx.agrobank.ui.activity.Main2Activity;
import com.sdcx.agrobank.ui.activity.Main3Activity;
import com.sdcx.agrobank.ui.activity.Main4Activity;
import com.sdcx.agrobank.ui.activity.ZCFZActivity;
import com.sdcx.agrobank.util.GlideUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Project   : haveyou
 * Package    : com.sdcx.agrobank.ui.fragment
 *
 * @author : Galaxy
 * @date : 2020/11/24 10:28
 * Desc      : TODO
 **/
public class wdFragment extends BaseFragment {

    private LinearLayout login,wdlayout1,wdlayout2,wdlayout3,wdlayout4;
    private ImageView setting;
    private TextView chakan,unlogin,loginname;
    private boolean frag=false;
    private TextView tv1;
    private String now;
    private String s;
    private EditText zichan;
    private EditText fuzhai;
    private NestedScrollView scll;


    @Override
    public void onLazyLoad() {

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onmyEvent(myEvent myEvent) {
        wdlayout1.setVisibility(View.GONE);
        wdlayout3.setVisibility(View.GONE);
        wdlayout2.setVisibility(View.VISIBLE);
        wdlayout4.setVisibility(View.VISIBLE);
     /*   login.setClickable(false);
        login.setEnabled(false);*/
        unlogin.setText("安全退出");
        loginname.setText("已登录");
        frag=true;
        tv1.setVisibility(View.VISIBLE);
    }
    @Override
    protected int initLayout() {
        return R.layout.wdfragment;
    }
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    @Override
    protected void init(View view) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        String s1 = SPUtils.getInstance().getString("zichan");
        String s2 = SPUtils.getInstance().getString("fuzhai");

        long time = Calendar.getInstance().getTimeInMillis();
        this.s = stampToDate(time + "");
        scll = view.findViewById(R.id.scll);
        login = view.findViewById(R.id.login_layout);
        wdlayout1 = view.findViewById(R.id.wdlayout1);
        wdlayout2 = view.findViewById(R.id.wdlayout2);
        wdlayout3 = view.findViewById(R.id.wdlayout3);
        wdlayout4 = view.findViewById(R.id.wdlayout4);
        chakan = view.findViewById(R.id.chakan);
        zichan = view.findViewById(R.id.wodezichan);
        fuzhai = view.findViewById(R.id.wodefuzhai);

        zichan.setText(s1);
        fuzhai.setText(s2);

        tv1 = view.findViewById(R.id.text1);
        tv1.setText("上次登录 "+ this.s);
        setting =view.findViewById(R.id.setting);
        unlogin = view.findViewById(R.id.unlogin);
        loginname = view.findViewById(R.id.login_name);
/*
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), SettingActivity.class));
            }
        });
*/
        zichan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                zichan.setCursorVisible(true);
            }
        });
        this.fuzhai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                wdFragment.this.fuzhai.setCursorVisible(true);
            }
        });

        scll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                scll.setFocusable(true);
                // 如果xml文件里面没设置，就需要在这里设置
                scll.setFocusableInTouchMode(true);
                scll.requestFocus();
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                zichan.setCursorVisible(false);
                wdFragment.this.fuzhai.setCursorVisible(false);
                SPUtils.getInstance().put("zichan",zichan.getText().toString());
                SPUtils.getInstance().put("fuzhai", wdFragment.this.fuzhai.getText().toString());
                return false;
            }
        });
/*
        scll.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                fuzhai.setFocusable(true);
                // 如果xml文件里面没设置，就需要在这里设置
                fuzhai.setFocusableInTouchMode(true);
                fuzhai.requestFocus();
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(fuzhai.getWindowToken(), 0);
                return false;
            }
        });
*/

        wdlayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), ZCFZActivity.class));

            }
        });
        chakan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), Main3Activity.class));
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!frag) {
                    startActivity(new Intent(getContext(), Main3Activity.class));
                }else {
                    startActivity(new Intent(getContext(), SettingActivity.class));
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initLogic() {

    }

    @Override
    public void initImmersionBar() {

    }

    @Override
    public boolean immersionBarEnabled() {
        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
